#include "function.h"
#include <fstream>
#include <iomanip>
using namespace std;


void GridGenerate(VarArray<Point, 2, 0> &grid, int np_in_xdir, int np_in_ydir) {
  double x_max{1.0}, y_max{0.5};
  double deltx{x_max / (np_in_xdir - 1)}, delty{1.0 / (np_in_ydir - 1)};
  grid.ReConstruct(np_in_xdir, np_in_ydir);

  Feta feta(6, 0.5, np_in_ydir, 5, np_in_xdir);
  for (int j = 0; j < np_in_ydir; ++j) {
    for (int i = 0; i < np_in_xdir; ++i) {
      grid(i, j).x = feta.getX(i);
      grid(i, j).y = feta(j);
    }
  }
}

void OutPutGridInPlot3d(const VarArray<Point, 2, 0> &grid) {
  ofstream os{"grid.x"};
  int np_in_xdir{grid.Dim(0)}, np_in_ydir{grid.Dim(1)};
  os << grid.Dim(0) << "  " << grid.Dim(1) << endl;
  os << scientific << setprecision(16);
  for (int j = 0; j < np_in_ydir; ++j) {
    for (int i = 0; i < np_in_xdir; ++i) {
      os << grid(i, j).x << " ";
      if ((i + 1) % 4 == 0)
        os << "\n";
    }
  }

  for (int j = 0; j < np_in_ydir; ++j) {
    for (int i = 0; i < np_in_xdir; ++i) {
      os << grid(i, j).y << " ";
      if ((i + 1) % 4 == 0)
        os << "\n";
    }
  }
}

void InitializeFlow(VarArray<Variable, 2, 0> &flow) {

  Variable zero{0.0, 0.0};
  flow = zero;
  const int np_in_ydir = flow.Dim(1);
  flow(0, 0).u = 0.0;
  flow(0, 0).v = 0.0;
  // 给第一列赋初值
  for (int j = 1; j < np_in_ydir; ++j) {
    flow(0, j).u = 1.0;
    flow(0, j).v = 0.0;
  }
  const int np_in_xdir{flow.Dim(0)};
  // 给下边界赋值
  for (int i = 1; i < np_in_xdir; ++i) {
    flow(i, 0).u = 0.0;
    flow(i, 0).v = 0.0;
  }
}

void InitializeNextLine(VarArray<Variable, 2, 0> &flow, int column) {
  int npoint_in_ydir{flow.Dim(1)};
  for (int j = 1; j < npoint_in_ydir; ++j) {
    flow(column, j) = flow(column - 1, j);
  }
}

